package com.hjx.movie.core.system.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjx.movie.core.system.runner.SystemStaticData;
import com.hjx.movie.core.system.service.admin.ICinemaHallSeatService;
import com.hjx.movie.persistence.system.mapper.IvyCinemaHallSeatMapper;
import com.hjx.movie.persistence.system.model.IvyCinemaHallSeat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/9 - 19:32
 */

@Service
@Transactional
@Slf4j
@CacheConfig(cacheNames = "IvyMovie:SpringCache:CinemaHallSeatCache",keyGenerator = "cacheKeyGenerator")
public class CinemaHallSeatService extends ServiceImpl<IvyCinemaHallSeatMapper, IvyCinemaHallSeat> implements ICinemaHallSeatService {
    @Resource
    private SystemStaticData staticData;

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinemaHallSeat> findByCinemaId(Long cinemaHallId) {
        return list(new QueryWrapper<IvyCinemaHallSeat>()
                .eq("cinema_hall_id", cinemaHallId)
                .ne("status", staticData.getStaticValue("STATUS_DELETE")));
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallSeatCache",key = "#root.targetClass.name+':findByCinemaId:'+#seat.cinemaHallId.toString()",beforeInvocation = true )
    @Override
    public boolean updateSeat(IvyCinemaHallSeat seat) {
        log.debug("修改影厅座位接受到的入参为==>{}",seat);
        return updateById(seat);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallSeatCache",key = "#root.targetClass.name+':findByCinemaId:'+#cinemaHallId.toString()",beforeInvocation = true )
    @Override
    public boolean generatorSeats(Long cinemaHallId, Integer maxX, Integer maxY) {
        // 不管原来有没有 将原本的都设置为未生效状态
        log.debug("生成影厅座位接收到的入参===>cinemaHallId:{},maxX:{},maxY:{}",cinemaHallId,maxX,maxY);
        logicDeleteSeatByCinemaHallId(cinemaHallId);
        List<IvyCinemaHallSeat> list = new ArrayList<>();
        for (int y=1;y<=maxY;y++){
            for (int x=1;x<=maxX;x++){
                list.add(IvyCinemaHallSeat.builder()
                        .cinemaHallId(cinemaHallId)
                        .seatX(x).seatY(y).seatType("N")
                        .status(Integer.parseInt(staticData.getStaticValue("STATUS_NORMAL"))).build());
            }
        }
        return saveBatch(list);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallSeatCache",key = "#root.targetClass.name+':findByCinemaId:'+#cinemaHallId.toString()",beforeInvocation = true )
    @Override
    public boolean realDeleteSeatByCinemaHallId(Long cinemaHallId) {
        return remove(new QueryWrapper<IvyCinemaHallSeat>().eq("cinema_hall_id",cinemaHallId));
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallSeatCache",key = "#root.targetClass.name+':findByCinemaId:'+#cinemaHallId.toString()",beforeInvocation = true )
    @Override
    public boolean logicDeleteSeatByCinemaHallId(Long cinemaHallId) {
        log.debug("逻辑删除座位");
        return update(IvyCinemaHallSeat.builder()
                .status(Integer.parseInt(staticData.getStaticValue("STATUS_DELETE"))).build(),
                new QueryWrapper<IvyCinemaHallSeat>()
                        .eq("cinema_hall_id",cinemaHallId)
                        .ne("status",staticData.getStaticValue("STATUS_DELETE")));
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaHallSeatCache",key = "#root.targetClass.name+':findByCinemaId:'+#cinemaHallId.toString()",beforeInvocation = true )
    public boolean updateSeatByCinemaHallId(Long cinemaHallId,Integer status) {
        log.debug("修改影厅座位状态接受到的入参为==>cinemaHallId:{},status:{}",cinemaHallId,status);
        return update(IvyCinemaHallSeat.builder().status(status).build(),new QueryWrapper<IvyCinemaHallSeat>().eq("cinema_hall_id",cinemaHallId));
    }

}
